import java.util.*;

public class Logic implements ILogic {

	int min;//minutes between two messages
	String[] msg;//array of messages
	String[] titles;//array of titles
	boolean[] exerType;//which type of exercises to show
	int currentExr;
	Timer timer;
	ExerTask et;
	private IGUI gui;

	public Logic (int min,String[] titles, String[] msg, int exerType){
		this.titles = titles;
		this.min = min;
		this.msg = msg;
		this.currentExr = 0;
		this.exerType = new boolean[exerType];
		for (int i = 0 ; i < exerType ; i++)
			this.exerType[i] = true;
	}
	public Logic(IGUI gui){
		int n = 2;
		min = 10;
		msg = new String[2];
		msg[0] = "do this";
		msg[1] = "do that";
		titles = new String[2];
		titles[0] = "תרגיל ראשון";
		titles[1] = "תרגיל שני";
		currentExr = 0;
		this.exerType = new boolean[n];
		for (int i = 0 ; i < n ; i++)
			this.exerType[i] = true;
	}

	//set the type of exercises to show
	public void setExercises(boolean[] exer) {
		this.exerType = exer;

	}

	//set the timer
	public void setTimer(int min) {
		this.shut();//problem when not init
		this.min = min;
		this.start();

	}

	//shut off the timer
	public void shut() {
		if(et != null)
			et.cancel();
	}

	//starts a timer
	public void start() {
		timer = new Timer();
		ExerTask et = new ExerTask(this.msg[this.currentExr], gui);
		this.nextExer();
		this.et = et;
		//timer.schedule(et, this.min*60000);
		timer.schedule(et, this.min*1000);//for debugging purpose only
	}

	//update the currentExr pointer
	public void nextExer() {
		boolean found = false;
		while (!found){
			this.currentExr = (this.currentExr++) % this.msg.length;
			if (this.exerType[this.currentExr]);
			found = true;
		}
	}

	//inner class, represent an exercise task
	class ExerTask extends TimerTask{
		String exerMsg;
		IGUI gui;
		public ExerTask(String msg, IGUI gui){
			this.exerMsg = msg;
			this.gui = gui;
		}
		public void run() {
			gui.printExc(getTitle() + "\n" + getInfo());
			gui.playSound();
			System.out.println(this.exerMsg);
		}
	}
	public String getInfo(){
		return (this.msg[this.currentExr]);
	}
	public String getTitle(){
		return (this.titles[this.currentExr]);
	}
	//NOT SURE!!!!!!!!!!!!!!
	public long getTime(){
		return (this.et.scheduledExecutionTime());
	}
	public static void main (String[] args){
		/*String[] s = {"as"};
		Logic l = new Logic(4,s,1);
		Logic l2 = new Logic(5,s,1);
		l.start();
		l2.start();
		System.out.println("vdsvdsv");*/
		//l.shut();
	}
}
